/**
 * Created by vincent on 2017/9/18.
 */
moduleStudyMap.controller("AddStudyClassController", function ($$CambioConfig, $scope, $log, $state, LoadingService, DialogService,
                                                               UploadFileService, EnumService, ConstantService, StudyClassResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
    });

    $scope.newStudyClass = {};

    /**
     * 上传图片
     * @param $files
     */
    $scope.uploadImage = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.newStudyClass.picture = o.id;
            });
        }
    };

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        var currentStudyClassGroupId = ConstantService.getItem("currentStudyClassGroupId");
        if ($scope.addSCForm.$valid && !StringUtils.isNlOrUndOrEmpty(currentStudyClassGroupId)) {
            //LoadingService.show();
            if (!StringUtils.isNlOrUndOrEmpty($scope.newStudyClass.typeOption)) {
                $scope.newStudyClass.type = EnumService.getEnumKey($scope.newStudyClass.typeOption);
            }
            $scope.newStudyClass.studyClassGroup = {id: currentStudyClassGroupId};
            StudyClassResource.add($scope.newStudyClass, function (classResult) {
                $log.debug("课程新增结果", classResult);
                $scope.classList.push(classResult);
                DialogService.close();
                //LoadingService.hide();
                Alert.success("新增课程成功！");
            }, function () {
                //LoadingService.hide();
            });
        }
    };

});